package toubiao.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import toubiao.domain.vo.assist.Json;
import toubiao.domain.vo.business.ContractPhoto;
import toubiao.exception.CustomException;
import toubiao.service.impl.PhotoServiceImpl;
import toubiao.utils.ProUtilConfig;
import toubiao.utils.ProUtilLogger;
import utils.UtilCollection;
import utils.UtilController;
import utils.UtilString;

@Controller
@RequestMapping("/photo")
public class PhotoController {
	
	private Logger logger=ProUtilLogger.getRootLogger();
	
	private String resourceBasePath=ProUtilConfig.getResourceBasePath();
	
	@Autowired
	private PhotoServiceImpl photoService;
	
	@RequestMapping("/project/contractPhotoList.json")
	@ResponseBody
	public Json jsonGetContractPhotoListOfProjectByProjectId(String projectId) throws InstantiationException, IllegalAccessException{
		logger.info("project = "+projectId);
		return photoService.getContractPhotoListOfProjectByProjectId(projectId);
	}
	
	
	//photoType: contractPhoto
	@RequestMapping("/get/{photoType}/{photoId}.json")
	@ResponseBody
	public Json jsonGetPhotoById(@PathVariable("photoType")String photoType,@PathVariable("photoId")String photoId) throws InstantiationException, IllegalAccessException{
		return photoService.getPhotoById(photoType,photoId);
	}
	
	//photoType: contractPhoto
	@RequestMapping("/operate/{photoType}/{opertorType}.json")
	@ResponseBody
	public Json jsonOperatePhoto(@PathVariable("photoType")String photoType,@PathVariable("opertorType")String opertorType,ContractPhoto contractPhoto,HttpServletRequest request) throws CustomException, InstantiationException, IllegalAccessException{
		Set<String> keySet=request.getParameterMap().keySet();
		String[] filedeNames=UtilCollection.collection2Array(String.class, keySet);
		logger.info(keySet.toString());
		return photoService.operatePhoto(opertorType,contractPhoto,filedeNames);
	}
	
	
	
	
	/*@RequestMapping(value = "/file/{webPath}")
	public void getImage(HttpServletRequest request,HttpServletResponse response,@PathVariable("webPath")String webPath) {
		//直接获得webPath 会 将文件的后缀名去掉,如 urL:/file/abc.jpg  webPath=abc,因此需要通过获取uri或者url 获取文件的后缀名
		//logger.info(webPath);
		//logger.info(request.getRequestURI());
		
		String uri=request.getRequestURI();
		String suffixOfImageFile=UtilString.getLastSubString(uri, ".");
		webPath=webPath+"."+suffixOfImageFile;
		logger.info(webPath);
		String fileAbsPath="d:/temp/"+webPath;
		UtilController.getImage(fileAbsPath, request, response);
	}*/
}
